<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="formData.orderNo" placeholder="请输入订单号"/>
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="formData.userId" placeholder="请输入用户id"/>
        </el-form-item>
        <el-form-item label="中台用户id" prop="userId">
          <el-input v-model="formData.ztUserId" placeholder="请输入中台用户id"/>
        </el-form-item>
        <el-form-item label="快递公司id" prop="courierId">
          <el-input v-model="formData.courierId" placeholder="请输入快递公司id"/>
        </el-form-item>
        <el-form-item label="快递公司名称" prop="courierName">
          <el-input v-model="formData.courierName" placeholder="请输入快递公司名称"/>
        </el-form-item>
        <el-form-item label="所属部门" prop="department">
          <el-input v-model="formData.department" placeholder="请输入所属部门"/>
        </el-form-item>
        <el-form-item label="所属单位" prop="unit">
          <el-input v-model="formData.unit" placeholder="请输入所属单位"/>
        </el-form-item>
        <el-form-item label="寄件人姓名" prop="senderName">
          <el-input v-model="formData.senderName" placeholder="请输入寄件人姓名"/>
        </el-form-item>
        <el-form-item label="寄件人电话" prop="senderPhone">
          <el-input v-model="formData.senderPhone" placeholder="请输入寄件人电话"/>
        </el-form-item>
        <el-form-item label="寄件地址" prop="senderAddress">
          <el-input v-model="formData.senderAddress" placeholder="请输入寄件地址"/>
        </el-form-item>
        <el-form-item label="收件人姓名" prop="receiverName">
          <el-input v-model="formData.receiverName" placeholder="请输入收件人姓名"/>
        </el-form-item>
        <el-form-item label="收件人电话" prop="receiverPhone">
          <el-input v-model="formData.receiverPhone" placeholder="请输入收件人电话"/>
        </el-form-item>
        <el-form-item label="收件地址" prop="receiverAddress">
          <el-input v-model="formData.receiverAddress" placeholder="请输入收件地址"/>
        </el-form-item>
        <el-form-item label="物品清单" prop="itemsList">
          <el-input v-model="formData.itemsList" placeholder="请输入物品清单"/>
        </el-form-item>
        <el-form-item label="快件类型" prop="itemsType">
          <el-select v-model="formData.itemsType" placeholder="请选择快件类型">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="快件预估重量(kg)" prop="itemsEstimatedWeight">
          <el-input v-model="formData.itemsEstimatedWeight" placeholder="请输入快件预估重量(kg)"/>
        </el-form-item>
        <el-form-item label="快件实际重量(kg)" prop="itemsActualWeight">
          <el-input v-model="formData.itemsActualWeight" placeholder="请输入快件实际重量(kg)"/>
        </el-form-item>
        <el-form-item label="快件体积" prop="itemsVolume">
          <el-input v-model="formData.itemsVolume" placeholder="请输入快件体积"/>
        </el-form-item>
        <el-form-item label="优惠前费用" prop="beforeCouponFee">
          <el-input v-model="formData.beforeCouponFee" placeholder="请输入优惠前费用"/>
        </el-form-item>
        <el-form-item label="优惠费用" prop="couponFee">
          <el-input v-model="formData.couponFee" placeholder="请输入优惠费用"/>
        </el-form-item>
        <el-form-item label="优惠后费用(总费用)" prop="totalFee">
          <el-input v-model="formData.totalFee" placeholder="请输入优惠后费用(总费用)"/>
        </el-form-item>
        <el-form-item label="是否需要补差价" prop="needCompensation">
<!--          <el-input v-model="formData.needCompensation" placeholder="请输入优惠后费用(总费用)"/>-->
          <el-radio-group v-model="formData.needCompensation">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING)"
                      :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="补差价金额" prop="compensationAmount">
          <el-input v-model="formData.compensationAmount" placeholder="请输入补差价金额"/>
        </el-form-item>
        <el-form-item label="期望上门时间" prop="expectTime">
          <el-date-picker clearable v-model="formData.expectTime" type="date" value-format="timestamp"
                          placeholder="选择期望上门时间"/>
        </el-form-item>
        <el-form-item label="备注" prop="orderRemark">
          <el-input v-model="formData.orderRemark" placeholder="请输入备注"/>
        </el-form-item>
        <el-form-item label="是否挂账(默认0：不挂账；1：挂账)" prop="putAccount">
          <el-input v-model="formData.putAccount" placeholder="请输入是否挂账(默认0：不挂账；1：挂账)"/>
        </el-form-item>
        <el-form-item label="下单时间" prop="orderTime">
          <el-date-picker clearable v-model="formData.orderTime" type="date" value-format="timestamp"
                          placeholder="选择下单时间"/>
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable v-model="formData.payTime" type="date" value-format="timestamp"
                          placeholder="选择支付时间"/>
        </el-form-item>
        <el-form-item label="取件时间" prop="pickupTime">
          <el-date-picker clearable v-model="formData.pickupTime" type="date" value-format="timestamp"
                          placeholder="选择取件时间"/>
        </el-form-item>
        <el-form-item label="确认收货时间" prop="confirmTime">
          <el-date-picker clearable v-model="formData.confirmTime" type="date" value-format="timestamp"
                          placeholder="选择确认收货时间"/>
        </el-form-item>
        <el-form-item label="当前物流状态(已下单、运输中、代取件、已签收)" prop="deliverStatus">
          <el-radio-group v-model="formData.deliverStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="支付状态(待支付、支付中、已支付)" prop="payStatus">
          <el-radio-group v-model="formData.payStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="开票状态(未开票、开票中、已开票)" prop="invoiceStatus">
          <el-radio-group v-model="formData.invoiceStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="支付方式" prop="payMethod">
          <el-input v-model="formData.payMethod" placeholder="请输入支付方式"/>
        </el-form-item>
        <el-form-item label="版本号" prop="version">
          <el-input v-model="formData.version" placeholder="请输入版本号"/>
        </el-form-item>
        <el-form-item label="异常类型(延误、丢件)" prop="errorType">
          <el-select v-model="formData.errorType" placeholder="请选择异常类型(延误、丢件)">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="异常描述">
          <Editor v-model="formData.errorDescription" :min-height="192"/>
        </el-form-item>
        <el-form-item label="是否解决(默认false)" prop="isResolved">
          <el-input v-model="formData.isResolved" placeholder="请输入是否解决(默认false)"/>
        </el-form-item>
        <el-form-item label="解决时间" prop="resolvedTime">
          <el-date-picker clearable v-model="formData.resolvedTime" type="date" value-format="timestamp"
                          placeholder="选择解决时间"/>
        </el-form-item>
        <el-form-item label="是否使用优惠券(默认0：未使用)" prop="useCoupon">
          <el-input v-model="formData.useCoupon" placeholder="请输入是否使用优惠券(默认0：未使用)"/>
        </el-form-item>
        <el-form-item label="使用的优惠券id" prop="couponId">
          <el-input v-model="formData.couponId" placeholder="请输入使用的优惠券id"/>
        </el-form-item>
        <el-form-item label="使用的优惠券类型(满减、会员折扣)" prop="couponType">
          <el-select v-model="formData.couponType" placeholder="请选择使用的优惠券类型(满减、会员折扣)">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="订单评分(1-5)" prop="evaluationScore">
          <el-input v-model="formData.evaluationScore" placeholder="请输入订单评分(1-5)"/>
        </el-form-item>
        <el-form-item label="评价类型(快递时效、服务态度、包装完整性)" prop="evaluationType">
          <el-select v-model="formData.evaluationType" placeholder="请选择评价类型(快递时效、服务态度、包装完整性)">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="文字建议" prop="evaluationAdvice">
          <el-input v-model="formData.evaluationAdvice" placeholder="请输入文字建议"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as DeliveryOrderApi from '../../../../api/ipark/delivery/deliveryorder';
import Editor from '@/components/Editor/index.vue';

export default {
  name: "DeliveryOrderForm",
  components: {
    Editor,
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        orderNo: undefined,
        userId: undefined,
        ztUserId:undefined,
        courierId: undefined,
        courierName: undefined,
        department: undefined,
        unit: undefined,
        senderName: undefined,
        senderPhone: undefined,
        senderAddress: undefined,
        receiverName: undefined,
        receiverPhone: undefined,
        receiverAddress: undefined,
        itemsList: undefined,
        itemsType: undefined,
        itemsEstimatedWeight: undefined,
        itemsActualWeight: undefined,
        itemsVolume: undefined,
        beforeCouponFee: undefined,
        couponFee: undefined,
        totalFee: undefined,
        needCompensation:undefined,
        compensationAmount:undefined,
        expectTime: undefined,
        orderRemark: undefined,
        putAccount: undefined,
        orderTime: undefined,
        payTime: undefined,
        pickupTime: undefined,
        confirmTime: undefined,
        deliverStatus: undefined,
        payStatus: undefined,
        invoiceStatus: undefined,
        payMethod: undefined,
        version: undefined,
        errorType: undefined,
        errorDescription: undefined,
        isResolved: undefined,
        resolvedTime: undefined,
        useCoupon: undefined,
        couponId: undefined,
        couponType: undefined,
        evaluationScore: undefined,
        evaluationType: undefined,
        evaluationAdvice: undefined,
      },
      // 表单校验
      formRules: {
        orderNo: [{required: true, message: '订单号不能为空', trigger: 'blur'}],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await DeliveryOrderApi.getDeliveryOrder(id);
          this.formData = res.data;
          this.title = "修改快递服务-订单";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增快递服务-订单";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await DeliveryOrderApi.updateDeliveryOrder(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await DeliveryOrderApi.createDeliveryOrder(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        orderNo: undefined,
        userId: undefined,
        courierId: undefined,
        courierName: undefined,
        department: undefined,
        unit: undefined,
        senderName: undefined,
        senderPhone: undefined,
        senderAddress: undefined,
        receiverName: undefined,
        receiverPhone: undefined,
        receiverAddress: undefined,
        itemsList: undefined,
        itemsType: undefined,
        itemsWeight: undefined,
        itemsVolume: undefined,
        beforeCouponFee: undefined,
        couponFee: undefined,
        totalFee: undefined,
        expectTime: undefined,
        orderRemark: undefined,
        putAccount: undefined,
        orderTime: undefined,
        payTime: undefined,
        pickupTime: undefined,
        confirmTime: undefined,
        deliverStatus: undefined,
        payStatus: undefined,
        invoiceStatus: undefined,
        payMethod: undefined,
        version: undefined,
        errorType: undefined,
        errorDescription: undefined,
        isResolved: undefined,
        resolvedTime: undefined,
        useCoupon: undefined,
        couponId: undefined,
        couponType: undefined,
        evaluationScore: undefined,
        evaluationType: undefined,
        evaluationAdvice: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
